from email.errors import HeaderMissingRequiredValue
import numpy as np
import cv2
import lmdb
from tqdm import tqdm
import six
from PIL import Image
from sklearn.utils import shuffle
import os
from glob import glob
lmdb_path= '/home/wsl/LMDB/MLT/MySynthMLT/Hindi'

env = lmdb.open(lmdb_path, max_readers=32, readonly=True, lock=False, readahead=False, meminit=False)
with env.begin(write=False) as txn:
    n_samples = int(txn.get('num-samples'.encode()))
    indexs=np.arange(1,10)
    for index in tqdm(indexs):
        label_key = 'label-%09d'.encode() % index
        label = txn.get(label_key).decode('utf-8')
        img_key = 'image-%09d'.encode() % index
        imgbuf = txn.get(img_key)

        buf = six.BytesIO()
        buf.write(imgbuf)
        buf.seek(0)
        img = Image.open(buf).convert('RGB')  # for color image
        img = np.array(img)
        #cv2.imwrite(os.path.join(tgt_dir,'%d.png'%index),img)
        print(label)
        cv2.imshow('a',img)
        cv2.waitKey(0)